package com.poisonh.unifiedauth.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.dialog.ChooseTimeDialogFragment;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.PreferencesUtils;
import com.zhy.autolayout.AutoRelativeLayout;

/**
 * 密码
 * Created by PoisonH on 2016/6/28.
 */
public class SettingCommondActivity extends BaseActivity implements View.OnClickListener, ChooseTimeDialogFragment.ChooseTimeListener
{
    private AutoRelativeLayout mArlSettingCommondA;
    private AutoRelativeLayout mArlSettingCommondB;
    private TextView mTvTime;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_settingcommond);
        initTitleView();
        initView();
    }


    @Override
    protected void initView()
    {
        mRightBtn.setVisibility(View.GONE);
        setTitleTxt("密码");
        mArlSettingCommondA = (AutoRelativeLayout) findViewById(R.id.arl_setting_commond_a);
        mArlSettingCommondB = (AutoRelativeLayout) findViewById(R.id.arl_setting_commond_b);
        mTvTime = (TextView) findViewById(R.id.tv_show_time);
        mArlSettingCommondA.setOnClickListener(this);
        mArlSettingCommondB.setOnClickListener(this);
        mLeftBtn.setOnClickListener(this);
        long time = PreferencesUtils.getLong(this, "LOCK_SCREEN_TIME",120);
        if (time != 0)
        {
            mTvTime.setText(showTxt(time));
        }
    }

    private String showTxt(long time)
    {
        String txt = null;
        if (time == 10001)
        {
            txt = "永不锁屏";
        }
        if (time == 15)
        {
            txt = "15秒之后自动锁屏";
        }
        if (time == 30)
        {
            txt = "30秒之后自动锁屏";
        }
        if (time == 60)
        {
            txt = "1分钟之后自动锁屏";
        }
        if (time == 120)
        {
            txt = "2分钟之后自动锁屏";
        }
        if (time == 300)
        {
            txt = "5分钟之后自动锁屏";
        }
        if (time == 600)
        {
            txt = "10分钟之后自动锁屏";
        }
        return txt;
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.arl_setting_commond_a:
                ConstantUitls.TYPE = 3;
                startActivity(new Intent(SettingCommondActivity.this, SettingPinCodeActivity.class));
                break;
            case R.id.arl_setting_commond_b:
                ChooseTimeDialogFragment chooseTimeDialogFragment = new ChooseTimeDialogFragment();
                chooseTimeDialogFragment.show(getFragmentManager(), "");
                break;
            case R.id.ib_left_btn:
                finish();
                break;
        }
    }

    @Override
    public void onChooseTimeListener(String time)
    {
        if (time.equals("永不"))
        {
            mTvTime.setText(time + "锁屏");
        } else
        {
            mTvTime.setText(time + "之后自动锁屏");
        }

        switch (time)
        {
            case "15秒":
                PreferencesUtils.putLong(this, "LOCK_SCREEN_TIME", 15);
                break;
            case "30秒":
                PreferencesUtils.putLong(this, "LOCK_SCREEN_TIME", 30);
                break;
            case "1分钟":
                PreferencesUtils.putLong(this, "LOCK_SCREEN_TIME", 60);
                break;
            case "2分钟":
                PreferencesUtils.putLong(this, "LOCK_SCREEN_TIME", 120);
                break;
            case "5分钟":
                PreferencesUtils.putLong(this, "LOCK_SCREEN_TIME", 300);
                break;
            case "10分钟":
                PreferencesUtils.putLong(this, "LOCK_SCREEN_TIME", 600);
                break;
            case "永不":
                PreferencesUtils.putLong(this, "LOCK_SCREEN_TIME", 10001);
                break;
        }

    }
}
